﻿using Dapper;
using Dapper.Extensions;
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ymatou.CommonService;
using Ymatou.ServiceCenter.Domain;
using Ymatou.ServiceCenter.Domain.IRepositories;
using Ymatou.ServiceCenter.Domain.Models;

namespace Ymatou.ServiceCenter.Repository
{
    public class ServiceMetadataRepository : RepositoryBase<ServiceMetadata>, IServiceMetadataRepository
    {
        public ServiceMetadata GetServiceMetadata(string appId, string apiName, string httpMethod)
        {
            return base.GetById("Default", "servicemetadata", new { AppId = appId, ApiName = apiName, HttpMethod = httpMethod });
        }

        public IList<ServiceMetadata> GetServiceMetadatas(string appId, long? lastUpdateTicks)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("AppId=@AppId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("AppId", appId, 50);

            if (lastUpdateTicks != null)
            {
                sql.Append(" AND LastUpdateTicks>@LastUpdateTicks");
                dynamicParameters.Add("LastUpdateTicks", lastUpdateTicks);
            }
            return base.GetList("Default", "servicemetadata", sql.ToString(), dynamicParameters);
        }

        public IList<string> GetAllAppIdList()
        {
            List<string> resultList = new List<string>();
            using (IDbContext db = new DbContext(GetConnectionName(null)))
            {
                IList<dynamic> list = db.Query("select DISTINCT (AppId) from servicemetadata");
                if (list != null && list.Count > 0)
                {
                    foreach (dynamic item in list)
                    {
                        if (item != null)
                            resultList.Add(item.AppId);
                    }
                }
            }
            return resultList;
        }

        public int TryInsert(ServiceMetadata entity)
        {
            try
            {
                this.Insert("Default", "servicemetadata", entity);
                return 1;
            }
            catch (MySqlException ex)
            {
                if (ex.Number == 1062) return -2;
                return -3;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error(string.Format("[TryInsertServiceMetadata]异常：{0}", ex.Message), ex);
                return -1;
            }
        }

        public bool TryUpdate(ServiceMetadata entity)
        {
            try
            {
                bool updateResult = this.Update("Default", "servicemetadata", entity);
                return updateResult;
            }
            catch (Exception ex)
            {
                ApplicationLog.Error(string.Format("[TryUpdateServiceMetadata]异常：{0}", ex.Message), ex);
                return false;
            }
        }
    }
}
